#!/bin/sh

echo "Thingino Diagnostics - sensor info"
echo "This script requires network access to download modules if needed..."

# Check if sinfo module is already loaded
if lsmod | grep -q "sinfo"; then
	echo "sinfo module is already loaded. Skipping download."
else
	# Get SoC type
	soc="$(soc -f)"
	if [ -z "$soc" ]; then
		echo "Error: Could not determine SoC type."
		exit 1
	fi

	echo "Detected SoC: $soc"
	echo "Downloading sinfo module..."

	# Download the appropriate module, check for kernel version later.
	curl -L "https://github.com/themactep/ingenic-sdk/releases/download/sinfo/sinfo-$soc.ko" -o "/tmp/sinfo-$soc.ko"
	if [ $? -ne 0 ]; then
		echo "Error: Failed to download sinfo module."
		exit 1
	fi

	# Load the module
	echo "Loading sinfo module..."
	insmod "/tmp/sinfo-$soc.ko"
	if [ $? -ne 0 ]; then
		echo "Error: Failed to load sinfo module."
		rm -f "/tmp/sinfo-$soc.ko"
		exit 1
	fi

	# Clean up downloaded file
	rm -f "/tmp/sinfo-$soc.ko"
fi

# Trigger info output
echo "Detecting sensors, please wait..."
echo 1 > /proc/jz/sinfo/info

# Check for module dir
if [ -f "/proc/jz/sinfo/info" ]; then
	sensor_proc=$(cat /proc/jz/sinfo/info)
	if echo "$sensor_proc" | grep -q "Detected sensors"; then
		echo ""
		echo "$sensor_proc"
		exit 0
	else
		echo "No sensor information found."
	fi
fi
